﻿Imports Microsoft.VisualBasic.FileIO.FileSystem
Imports System.IO
Imports System.Text
Imports System.Security.Principal

Module Synclogic


    Public Function IsUserAdministrator() As Boolean

        Dim isAdmin As Boolean

        Try

            Dim user As WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim principal As New WindowsPrincipal(user)

            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator)

        Catch ex As UnauthorizedAccessException

            isAdmin = False

        Catch ex As Exception

            isAdmin = False

        End Try

        Return isAdmin

    End Function

    Public Function FormatBytes(ByVal NumBytes As Double) As String

        Dim MyNumber As String
        Dim Counter As Integer

        Do While (NumBytes >= 1024 And Counter <= 3)

            NumBytes = NumBytes / 1024

            Counter += 1

        Loop

        NumBytes = Math.Round(NumBytes, 2)

        Select Case Counter

            Case 0
                MyNumber = NumBytes.ToString + " Bytes"
            Case 1
                MyNumber = NumBytes.ToString + " KB"
            Case 2
                MyNumber = NumBytes.ToString + " MB"
            Case Else
                MyNumber = NumBytes.ToString + " GB"

        End Select

        Return MyNumber

    End Function

    Public Sub LoadMatrices(ByVal DgView As DataGridView, ByRef DirectoriesSource() As String, ByRef DirectoriesDestination() As String)

        Dim Row As DataGridViewRow
        Dim MyCell As DataGridViewCell
        Dim CountRows As Integer

        ReDim Preserve DirectoriesSource(DgView.Rows.Count - 1)
        ReDim Preserve DirectoriesDestination(DgView.Rows.Count - 1)

        For Each Row In DgView.Rows

            For Each MyCell In Row.Cells

                If MyCell.ColumnIndex = 0 Then

                    DirectoriesSource(CountRows) = MyCell.Value.ToString

                Else
                    DirectoriesDestination(CountRows) = MyCell.Value.ToString

                End If

            Next
            CountRows = CountRows + 1
        Next

    End Sub

    Public Function ComparesDateDirectories(ByVal DgView As DataGridView, ByRef DirSource() As String, ByRef DirDestination() As String) As Boolean

        Dim DirectorySource As DirectoryInfo = Nothing
        Dim DirectoryDestination As DirectoryInfo = Nothing
        Dim MyValue As Boolean
        Dim MyRow As DataGridViewRow
        Dim MyCell As DataGridViewCell
        Dim Count As Integer

        MyValue = True

        For Each MyRow In DgView.Rows

            For Each MyCell In MyRow.Cells

                If MyCell.ColumnIndex = 0 Then
                    DirectorySource = New DirectoryInfo(MyCell.Value.ToString)
                Else
                    DirectoryDestination = New DirectoryInfo(MyCell.Value.ToString)
                End If

            Next

            If DirectoryDestination.LastWriteTime < DirectorySource.LastWriteTime Then

                ReDim Preserve DirSource(Count)
                ReDim Preserve DirDestination(Count)

                DirSource(Count) = DirectorySource.Name.ToString
                DirDestination(Count) = DirectoryDestination.Name.ToString
                MyValue = False

                Count = Count + 1

            End If

        Next

        Return MyValue

    End Function

    Public Function ConcatDirectories(ByVal Directorios() As String) As String

        Dim MyString As String = Nothing
        Dim Count As Int16

        For Count = 0 To CShort(Directorios.Count - 1)
            If Count = Directorios.Count - 1 Then
                MyString = MyString + Directorios(Count)
            Else
                MyString = MyString + Directorios(Count) + ","
            End If
        Next

        Return MyString

    End Function

    Public Function ConcatDirectoriesSource(ByVal DgView As DataGridView) As String

        Dim DirectoryDestination As DirectoryInfo = Nothing
        Dim DirDestination() As String = Nothing
        Dim MyRow As DataGridViewRow
        Dim MyCell As DataGridViewCell
        Dim Count As Int16 = 0

        For Each MyRow In DgView.Rows

            For Each MyCell In MyRow.Cells

                If Not MyCell.ColumnIndex = 0 Then
                    DirectoryDestination = New DirectoryInfo(MyCell.Value.ToString)
                End If

            Next

            ReDim Preserve DirDestination(Count)

            DirDestination(Count) = DirectoryDestination.Name.ToString

            Count = CShort(Count + 1)

        Next

        Return ConcatDirectories(DirDestination)

    End Function

    Public Function CheckDirectories(ByVal DgView As DataGridView, ByRef MyPath As String) As Boolean

        Dim MyRow As DataGridViewRow
        Dim MyCell As DataGridViewCell

        For Each MyRow In DgView.Rows

            For Each MyCell In MyRow.Cells

                If Not DirectoryExists(MyCell.Value.ToString) Then

                    MyPath = MyCell.Value.ToString
                    Return False

                End If

            Next

        Next

        Return True

    End Function

    Public Function FindRobocopyPath(ByRef RobocopyPath As String) As Boolean

        If IO.File.Exists(Environment.SystemDirectory.ToString & "\Robocopy.exe") Then

            RobocopyPath = Environment.SystemDirectory.ToString & "\Robocopy.exe"
            Return True

        Else

            RobocopyPath = ""
            Return False

        End If

    End Function

End Module
